
package client.inventory;


import constants.ItemConstants;
import org.jetbrains.annotations.NotNull;
import server.MapleItemInformationProvider;

import java.io.Serializable;
import java.util.*;
import java.util.concurrent.locks.ReentrantReadWriteLock;











 public class MapleInventory implements Iterable<Item>, Serializable {
    /*  14 */   private final transient ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    private static final long serialVersionUID = -7238868473236710891L;
       private Map<Short, Item> inventory;
       private MapleInventoryType type;
    /*  17 */   private short slotLimit = 0;












    public MapleInventory(MapleInventoryType type) {
        /*  29 */
        this.inventory = new LinkedHashMap<>();
        /*  30 */
        this.type = type;

    }


    public MapleInventory copy() {
        MapleInventory ret = new MapleInventory();
        ret.type = this.type;
        ret.slotLimit = this.slotLimit;
        ret.inventory = new LinkedHashMap();
        this.lock.readLock().lock();

        try {
            this.inventory.forEach((key, value) -> {
                Item var10000 = (Item) ret.inventory.put(key, value);
            });
        } finally {
            this.lock.readLock().unlock();
        }

        return ret;
    }




    public void addSlot(short slot) {
        /*  48 */
        this.slotLimit = (short) (this.slotLimit + slot);
        /*  49 */
        if (this.slotLimit > 128) {
            /*  50 */
            this.slotLimit = 128;

        }

    }



    public short getSlotLimit() {
        /*  55 */
        return this.slotLimit;

    }



    public void setSlotLimit(short slot) {
        /*  59 */
        if (slot > 128) {
            /*  60 */
            slot = 128;

        }
        /*  62 */
        this.slotLimit = slot;

    }







    public Item findById(int itemId) {
        /*  70 */
        this.lock.readLock().lock();

        try {
            /*  72 */
            for (Item item : this.inventory.values()) {
                /*  73 */
                if (item.getItemId() == itemId) {
                    /*  74 */
                    return item;

                }

            }
            /*  77 */
            return null;

        } finally {
            /*  79 */
            this.lock.readLock().unlock();

        }

    }



    public Item findByUniqueId(int itemId) {
        /*  84 */
        this.lock.readLock().lock();

        try {
            /*  86 */
            for (Item item : this.inventory.values()) {
                /*  87 */
                if (item.getUniqueId() == itemId) {
                    /*  88 */
                    return item;

                }

            }
            /*  91 */
            return null;

        } finally {
            /*  93 */
            this.lock.readLock().unlock();

        }

    }





    public Item findByInventoryId(long onlyId, int itemId) {
        /* 100 */
        this.lock.readLock().lock();

        try {
            /* 102 */
            for (Item item : this.inventory.values()) {
                /* 103 */
                if (item.getInventoryId() == onlyId && item.getItemId() == itemId) {
                    /* 104 */
                    return item;

                }

            }
            /* 107 */
            return findById(itemId);

        } finally {
            /* 109 */
            this.lock.readLock().unlock();

        }

    }




    public Item findByEquipOnlyId(long onlyId, int itemId) {
        /* 115 */
        for (Item item : this.inventory.values()) {
            /* 116 */
            if (item.getEquipOnlyId() == onlyId && item.getItemId() == itemId) {
                /* 117 */
                return item;

            }

        }
        /* 120 */
        return null;

    }



    public int countById(int itemId) {
        /* 124 */
        int possesed = 0;
        /* 125 */
        for (Item item : this.inventory.values()) {
            /* 126 */
            if (item.getItemId() == itemId) {
                /* 127 */
                possesed += item.getQuantity();

            }

        }
        /* 130 */
        return possesed;

    }



    public List<Item> listById(int itemId) {
        /* 134 */
        List<Item> ret = new ArrayList<>();
        /* 135 */
        for (Item item : this.inventory.values()) {
            /* 136 */
            if (item.getItemId() == itemId) {
                /* 137 */
                ret.add(item);

            }

        }
        /* 140 */
        if (ret.size() > 1) {
            /* 141 */
            Collections.sort(ret);

        }
        /* 143 */
        return ret;

    }






    public List<Item> listByEquipOnlyId(int equipOnlyId) {
        /* 150 */
        List<Item> ret = new ArrayList<>();
        /* 151 */
        for (Item item : this.inventory.values()) {
            /* 152 */
            if (item.getEquipOnlyId() > 0 && item.getEquipOnlyId() == equipOnlyId) {
                /* 153 */
                ret.add(item);

            }

        }
        /* 156 */
        if (ret.size() > 1) {
            /* 157 */
            Collections.sort(ret);

        }
        /* 159 */
        return ret;

    }



    public Collection<Item> list() {
        /* 163 */
        return this.inventory.values();

    }



    public List<Item> newList() {
        /* 167 */
        if (this.inventory.size() <= 0) {
            /* 168 */
            return Collections.emptyList();

        }
        /* 170 */
        return new LinkedList<>(this.inventory.values());

    }



    public List<Integer> listIds() {
        /* 174 */
        List<Integer> ret = new ArrayList<>();
        /* 175 */
        for (Item item : this.inventory.values()) {
            /* 176 */
            if (!ret.contains(Integer.valueOf(item.getItemId()))) {
                /* 177 */
                ret.add(Integer.valueOf(item.getItemId()));

            }

        }
        /* 180 */
        if (ret.size() > 1) {
            /* 181 */
            Collections.sort(ret);

        }
        /* 183 */
        return ret;

    }






    public short addItem(Item item) {
        /* 190 */
        short slotId = getNextFreeSlot();
        /* 191 */
        if (slotId < 0) {
            /* 192 */
            return -1;

        }
        /* 194 */
        this.inventory.put(Short.valueOf(slotId), item);
        /* 195 */
        item.setPosition(slotId);
        /* 196 */
        return slotId;

    }



    public void addFromDB(Item item) {
        /* 200 */
        if (item.getPosition() < 0 && !this.type.equals(MapleInventoryType.EQUIPPED)) {

            return;

        }

        /* 204 */
        if (item.getPosition() > 0 && this.type.equals(MapleInventoryType.EQUIPPED)) {

            return;

        }

        /* 208 */
        this.inventory.put(Short.valueOf(item.getPosition()), item);

    }



    public void move(short sSlot, short dSlot, short slotMax) {
        /* 212 */
        Item source = this.inventory.get(Short.valueOf(sSlot));
        /* 213 */
        Item target = this.inventory.get(Short.valueOf(dSlot));
        /* 214 */
        if (source == null) {
            /* 215 */
            throw new InventoryException("Trying to move empty slot");

        }
        /* 217 */
        if (target == null) {
            /* 218 */
            if (dSlot < 0 && !this.type.equals(MapleInventoryType.EQUIPPED)) {

                return;

            }

            /* 222 */
            if (dSlot > 0 && this.type.equals(MapleInventoryType.EQUIPPED)) {

                return;

            }

            /* 226 */
            source.setPosition(dSlot);
            /* 227 */
            this.inventory.put(Short.valueOf(dSlot), source);
            /* 228 */
            this.inventory.remove(Short.valueOf(sSlot));
            /* 229 */
        } else if (target.getItemId() == source.getItemId() && !ItemConstants.isRechargable(source.getItemId()) && target.getOwner().equals(source.getOwner()) && target.getExpiration() == source.getExpiration() && target.getFamiliarCard() == null && source.getFamiliarCard() == null) {
            /* 230 */
            if (this.type.getType() == MapleInventoryType.EQUIP.getType() || this.type.getType() == MapleInventoryType.CASH.getType()) {
                /* 231 */
                swap(target, source);
                /* 232 */
            } else if (source.getQuantity() + target.getQuantity() > slotMax) {
                /* 233 */
                source.setQuantity((short) (source.getQuantity() + target.getQuantity() - slotMax));
                /* 234 */
                target.setQuantity(slotMax);

            } else {
                /* 236 */
                target.setQuantity((short) (source.getQuantity() + target.getQuantity()));
                /* 237 */
                this.inventory.remove(Short.valueOf(sSlot));

            }

        } else {
            /* 240 */
            swap(target, source);

        }

    }



    public void swap(Item source, Item target) {
        /* 245 */
        this.inventory.remove(Short.valueOf(source.getPosition()));
        /* 246 */
        this.inventory.remove(Short.valueOf(target.getPosition()));
        /* 247 */
        short swapPos = source.getPosition();
        /* 248 */
        source.setPosition(target.getPosition());
        /* 249 */
        target.setPosition(swapPos);
        /* 250 */
        this.inventory.put(Short.valueOf(source.getPosition()), source);
        /* 251 */
        this.inventory.put(Short.valueOf(target.getPosition()), target);

    }



    public Item getItem(short slot) {
        /* 255 */
        return this.inventory.get(Short.valueOf(slot));

    }



    public void removeItem(short slot) {
        /* 259 */
        removeItem(slot, (short) 1, false);

    }



    public void removeItem(short slot, short quantity, boolean allowZero) {
        /* 263 */
        Item item = this.inventory.get(Short.valueOf(slot));
        /* 264 */
        if (item == null) {

            return;

        }
        /* 267 */
        item.setQuantity((short) (item.getQuantity() - quantity));
        /* 268 */
        if (item.getQuantity() < 0) {
            /* 269 */
            item.setQuantity((short) 0);

        }
        /* 271 */
        if (item.getQuantity() == 0 && !allowZero) {
            /* 272 */
            removeSlot(slot);

        }

    }



    public void removeSlot(short slot) {
        /* 277 */
        this.inventory.remove(Short.valueOf(slot));

    }



    public void removeAll() {
        /* 281 */
        this.inventory.clear();

    }



    public boolean isFull() {
        /* 285 */
        return (this.inventory.size() >= this.slotLimit);

    }



    public boolean isFull(int margin) {
        /* 289 */
        return (this.inventory.size() + margin >= this.slotLimit);

    }






    public short getNextFreeSlot() {
        /* 296 */
        if (isFull()) {
            /* 297 */
            return -1;

        }
        /* 299 */
        for (short i = 1; i <= this.slotLimit; i = (short) (i + 1)) {
            /* 300 */
            if (!this.inventory.containsKey(Short.valueOf(i))) {
                /* 301 */
                return i;

            }

        }
        /* 304 */
        return -1;

    }



    public short getNumFreeSlot() {
        /* 308 */
        if (isFull()) {
            /* 309 */
            return 0;

        }
        /* 311 */
        short free = 0;
        /* 312 */
        for (short i = 1; i <= this.slotLimit; i = (short) (i + 1)) {
            /* 313 */
            if (!this.inventory.containsKey(Short.valueOf(i))) {
                /* 314 */
                free = (short) (free + 1);

            }

        }
        /* 317 */
        return free;

    }






    public List<Integer> listSkillSkinIds() {
        /* 324 */
        List<Integer> ret = new ArrayList<>();
        /* 325 */
        for (Item item : this.inventory.values()) {
            /* 326 */
            if (item.isSkillSkin() && !ret.contains(Integer.valueOf(item.getItemId()))) {
                /* 327 */
                ret.add(Integer.valueOf(item.getItemId()));

            }

        }
        /* 330 */
        return ret;

    }



    public final Item getArrowSlot(int n2) {
        /* 334 */
        this.lock.readLock().lock();

        try {
            /* 336 */
            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
            /* 337 */
            LinkedList<Item> items = new LinkedList<>();
            /* 338 */
            for (Item item : this.inventory.values()) {
                /* 339 */
                if (item.getPosition() <= 128) {
                    /* 340 */
                    items.add(item);

                }

            }
            /* 343 */
            items.sort(Comparator.naturalOrder());
            /* 344 */
            for (Item item : items) {
                /* 345 */
                if (ItemConstants.is弓矢道具(item.getItemId()) && n2 >= ii.getReqLevel(item.getItemId())) {
                    /* 346 */
                    return item;

                }

            }
            /* 349 */
            return null;

        } finally {
            /* 351 */
            this.lock.readLock().unlock();

        }

    }



    public final Item getCrossbowSlot(int n2) {
        /* 356 */
        this.lock.readLock().lock();

        try {
            /* 358 */
            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
            /* 359 */
            LinkedList<Item> items = new LinkedList<>();
            /* 360 */
            for (Item item : this.inventory.values()) {
                /* 361 */
                if (item.getPosition() > 128) {
                    items.add(item);
                }

            }
            /* 364 */
            items.sort(Comparator.naturalOrder());
            /* 365 */
            for (Item item : items) {
                /* 366 */
                if (!ItemConstants.is弩矢道具(item.getItemId()) || n2 < ii.getReqLevel(item.getItemId()))
                    /* 367 */ {
                    return item;
                }

            }
            /* 369 */
            return null;

        } finally {
            /* 371 */
            this.lock.readLock().unlock();

        }

    }



    public final Item getBulletSlot(int n2) {
        /* 376 */
        this.lock.readLock().lock();

        try {
            /* 378 */
            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
            /* 379 */
            LinkedList<Item> items = new LinkedList<>();
            /* 380 */
            for (Item item : this.inventory.values()) {
                /* 381 */
                if (item.getPosition() <= 128) {
                    /* 382 */
                    items.add(item);

                }

            }
            /* 385 */
            items.sort(Comparator.naturalOrder());
            /* 386 */
            for (Item item : items) {
                /* 387 */
                if (!ItemConstants.is子弹道具(item.getItemId()) || n2 < ii.getReqLevel(item.getItemId())){
                    return item;
                }
                    /* 388 */


            }
            /* 390 */
            return null;

        } finally {
            /* 392 */
            this.lock.readLock().unlock();

        }

    }








    public final Item getDartsSlot(int n2) {
        /* 402 */
        this.lock.readLock().lock();

        try {
            /* 404 */
            MapleItemInformationProvider ii = MapleItemInformationProvider.getInstance();
            /* 405 */
            LinkedList<Item> linkedList = new LinkedList<>();
            /* 406 */
            for (Item item : this.inventory.values()) {
                /* 407 */
                if (item.getPosition() <= 128) {
                    /* 408 */
                    linkedList.add(item);

                }

            }
            /* 411 */
            linkedList.sort(Comparator.naturalOrder());
            /* 412 */
            for (Item item : linkedList) {
                /* 413 */
                if (!ItemConstants.is飞镖道具(item.getItemId()) || n2 < ii.getReqLevel(item.getItemId())){
                    return item;
                }

            }
            /* 416 */
            return null;

        } finally {
            /* 418 */
            this.lock.readLock().unlock();

        }

    }



    public MapleInventoryType getType() {
        /* 423 */
        return this.type;

    }




    @Override
    @NotNull
    public Iterator<Item> iterator() {
        /* 428 */
        return Collections.<Item>unmodifiableCollection(this.inventory.values()).iterator();

    }

    public MapleInventory() {
    }

}


/* Location:              C:\PGX\Server.jar!\client\inventory\MapleInventory.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */